<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------

/**
 * Class CostAllocation
 * Desc  城市费用配置Model
 * Create on 2024/10/30 9:04
 * Create by wangyafang
 */
namespace app\api\model\wddj\user\operation;

use app\api\model\wddj\user\Order;
use app\api\model\wddj\WaitingTime;
use think\Model;

class CostAllocation extends Model
{
// 表名
    protected $name = 'wddj_cost_allocation';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    public static function estimated_amount($city_id,$routes_distance,$order_id = 0)
    {
        $estimatedAmount = 0;
        $startPriceModel = new StartingPrice();
        $currentTime = date('H:i:s',time());
        $where['start_time'] = array('elt',$currentTime);
        $where['end_time'] = array('egt',$currentTime);
        $cityWhere['city_id'] = array('eq',$city_id);
        $startPriceObj = $startPriceModel->where($where)->where($cityWhere)->find();
        $costAllocationObj = self::where($cityWhere)->find();
        $list = array();

        //计算起步价
        if ($startPriceObj) {
            $estimatedAmount = $startPriceObj['price'];
            $list[] = array(
                'title'=>'起步价(含'.$startPriceObj['kilometer'].'公里)',
                'desc'=>$startPriceObj['start_time'].' - '.$startPriceObj['end_time'].'时段 (含'.$startPriceObj['kilometer'].'公里) '.$startPriceObj['price'].'元',
                'price'=>$startPriceObj['price'],
            );
        }

        //计算里程费
        $routes_kilometer = bcdiv($routes_distance,1000,2);//总路程公里数
        if (!empty($startPriceObj) && isset($startPriceObj['kilometer'])) {
            $mileage_fee_kilometer = bcsub($routes_kilometer,$startPriceObj['kilometer'],1);//减掉起步价剩余的里程数
        } else {
            $mileage_fee_kilometer = $routes_kilometer;//减掉起步价剩余的里程数
        }

        if ($mileage_fee_kilometer >0) {
//            echo '里程未四舍五入：'.$mileage_fee_kilometer;
            $mileage_fee_kilometer_ceil = ceil($mileage_fee_kilometer);
//            echo '里程已四舍五入：'.$mileage_fee_kilometer_ceil;
            $mileage_fee_total_price = bcmul($mileage_fee_kilometer_ceil,$costAllocationObj['mileage_price'],2);
            $estimatedAmount = bcadd($estimatedAmount,$mileage_fee_total_price,2);
//            echo '里程费:'.$mileage_fee_total_price.'++++++++++++++++++';
            $list[] = array(
                'title'=>'里程费(超起步'.$mileage_fee_kilometer.'公里)',
                'desc'=>$costAllocationObj['mileage_fee_start_time'].' - '.$costAllocationObj['mileage_fee_end_time'].'时段 ('.$mileage_fee_kilometer.'公里) '.$mileage_fee_total_price.'元',
                'price'=>$mileage_fee_total_price,
                'mileage_fee_kilometer'=>$mileage_fee_kilometer,
            );
        }

        //远途费
        $sub_long_distance_kilometer = $long_distance_kilometer = bcsub($mileage_fee_kilometer,$costAllocationObj['exceeding_kilometers'],1);//超过30公里剩余公里数,//减掉起步价,里程费剩余的里程数
        if ($long_distance_kilometer >0) {
            $long_distance_kilometer = bcsub($mileage_fee_kilometer,$costAllocationObj['exceeding_kilometers'],1);//超过30公里剩余公里数
            $long_distance_kilometer = ceil($long_distance_kilometer);
            $long_distance_total_price = bcmul($long_distance_kilometer,$costAllocationObj['long_distance_price'],2);
            if ($long_distance_total_price > $costAllocationObj['capped_price']) {
//                echo '大于'.$costAllocationObj['capped_price'].'封顶,价格：'.$long_distance_total_price.'ffffffffffffffffffff';
                $long_distance_total_price = $costAllocationObj['capped_price'];
                $desc = '起步里程之上超过'.$costAllocationObj['exceeding_kilometers'].'公里('.$sub_long_distance_kilometer.'公里)，远途费超过封顶费：'.$costAllocationObj['capped_price'];
            } else {
                $desc = '起步里程之上超过'.$costAllocationObj['exceeding_kilometers'].'公里('.$sub_long_distance_kilometer.'公里)';
            }
            $estimatedAmount = bcadd($estimatedAmount,$long_distance_total_price,2);
//            echo '远途费:'.$long_distance_total_price.'********************';
            $list[] = array(
                'title'=>'远途费('.$sub_long_distance_kilometer.'公里)',
                'desc'=>$desc,
                'price'=>$long_distance_total_price,
            );
        }

        if ($order_id > 0) {
            $total_minute = (new WaitingTime())->where('order_id',$order_id)->where('minute','>',0)->sum('minute');
            $orderObj = (new Order())->get($order_id);
            if (!empty($total_minute)) {
                $duration_price = (new CostAllocation())->where('city_id',$orderObj['city_id'])->value('duration_price');
                $price = bcmul($duration_price,$total_minute,2);
                $estimatedAmount = bcadd($estimatedAmount,$price,2);
            }
        }

        return array(
            'estimated_amount'=>$estimatedAmount,
            'estimated_amount_details'=>$list,
        );

    }

    public static function get_actual_amount($order_id,$actual_amount)
    {


        $total_minute = (new WaitingTime())->where('order_id',$order_id)->where('minute','>',0)->sum('minute');
        $orderObj = (new Order())->get($order_id);
//        if (!empty($total_minute)) {
//            $duration_price = (new CostAllocation())->where('city_id',$orderObj['city_id'])->value('duration_price');
//            $price = bcmul($duration_price,$total_minute,2);
//            $actual_amount = bcadd($actual_amount,$price,2);
//        }

        return array(
            'estimated_amount'=>$actual_amount,
        );


    }


}



 